//
// Created by 1065246 on 2023/11/16.
//
#include<stdio.h>
#include<stdlib.h>
#include "link.h"

/**
 * 升序前插法
 * @param links
 * @param data
 */
void insertDataAsc(Links * links,int data){
    //构造新的节点
    Node* newNode = malloc(sizeof (Node));
    newNode->data = data;
    links->lenth++;

    //获取头结点,使用二级指针保存头结点变量本省的地址
    Node** pp= &links->head;
    //*pp当做右值,就是他的值传递别人
    Node* current = *pp;
    while (current && current->data > data) {
        //current指向下一个
        pp = &current->next;
        current = *pp;
    }

    newNode->next = current;
    //*PP当做左值，就是指针的值被别人改变
    *pp = newNode;
}

/**
 * 升序前插法
 * @param links
 * @param data
 */
void insertDatasSimpleAsc(Links * links, int data) {
    Node* newNode =malloc(sizeof (Node));
    newNode->data = data;
    links->lenth++;
    //获取头结点,使用二级指针保存
    Node** pp= &links->head;
    //二级指针解引用，就是一级指针
    Node* current = *pp;
    Node* prev = NULL;

    while (current && current->data > data) {
        prev = current;
        current = current->next;
    }
    newNode->next = current;
    if (prev) {
        prev->next = newNode;
    }
    else {
        *pp = newNode;
    }
}
